package io;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class FileDemo03 {
    public static void main(String[] args) throws IOException {
        Scanner scanner=new Scanner(System.in);
        String path=scanner.next();
        File root = new File(path);
        if(!root.isDirectory()){
            System.out.println("这个是非法文件目录");
            return;
        }
        System.out.println("请输入查找关键字");
        String key=scanner.next();
        List<File> result=new ArrayList<>();
        searchpath(root,key,result);
        System.out.println("共找到"+result.size()+"个结果，分别是");
        for (File file1:result
             ) {

            System.out.println(file1.getCanonicalPath()+"请问是否要删除文件y/n");
            String in= scanner.next();
            if(in.toLowerCase().equals("y")){
                file1.delete();
            }
        }

    }
    public static void searchpath(File root,String key,List<File> result){
        File[] files=root.listFiles();
        if(files == null){
            return;
        }
        for (File file:files
             ) {
            if(file.isDirectory()){
                searchpath(file,key,result);
            }else{
                if(file.getName().contains(key)){

                    result.add(file.getAbsoluteFile());
                }
            }
        }
    }
}
